package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author YYP
 * @date 2020/12/27
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    private CategoryBrandService categoryBrandService;

    @Autowired
    public CategoryServiceImpl(CategoryBrandService categoryBrandService){
        this.categoryBrandService = categoryBrandService;
    }

    /**
     * 根据品牌id集合查询，对应的分类集合
     * @param brandId
     * @return
     */
    @Override
    public List<CategoryDTO> queryCategoryByBrandId(Long brandId) {
        //sql
        //List<Category> categoryList = baseMapper.queryCategoryByBrandId(brandId);


        //1.根据品牌id，查询中间表，得到中间表对象集合
        List<CategoryBrand> categoryBrandList = categoryBrandService.query().eq("brand_id", brandId).list();
        if (CollectionUtils.isEmpty(categoryBrandList)) {
            return Collections.emptyList();
        }
        //2.获取分类id集合
        List<Long> categoryIdList = categoryBrandList.stream().map(CategoryBrand::getCategoryId).collect(Collectors.toList());
        //3.根据分类id集合，查询分类对象集合
        List<Category> categories = this.listByIds(categoryIdList);
        // 4.转换DTO
        return CategoryDTO.convertEntityList(categories);

    }

    /**
     * 根据分类id集合查询分类集合
     * @param ids
     * @return
     */
    @Override
    public List<CategoryDTO> listCategoryByIds(List<Long> ids) {
        List<Category> categoryList = this.listByIds(ids);
        List<CategoryDTO> categoryDTOList = CategoryDTO.convertEntityList(categoryList);
        return categoryDTOList;
    }

    /**
     * 根据分类id，查询分类
     * @param id
     * @return
     */
    @Override
    public CategoryDTO queryCategoryById(Long id) {
        Category category = this.getById(id);
        CategoryDTO categoryDTO = new CategoryDTO(category);
        return categoryDTO;
    }

    /**
     * 根据父id查询所有分类
     * @param parentId
     * @return
     */
    @Override
    public List<CategoryDTO> listCategoryByPid(Long parentId) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        List<Category> categoryList = this.list(wrapper);
        //List<Category> categoryList = this.query().eq("parent_id", parentId).list();
        List<CategoryDTO> categoryDTOList = CategoryDTO.convertEntityList(categoryList);
        return categoryDTOList;
    }
}
